package servlet;

import entity.MeetingRoom;
import service.MeetingRoomService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addRoom")
public class AddMeetingRoomServlet extends HttpServlet {
    private MeetingRoomService roomService = new MeetingRoomService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取表单参数
        String name = req.getParameter("name");
        Integer number = Integer.parseInt(req.getParameter("number"));
        String dept = req.getParameter("dept");
        String local = req.getParameter("local");
        Integer state = Integer.parseInt(req.getParameter("state"));

        //封装实体类
        MeetingRoom room = new MeetingRoom();
        room.setName(name);
        room.setNumber(number);
        room.setDept(dept);
        room.setLocal(local);
        room.setState(state);

        //调用业务层添加
        boolean isSuccess = roomService.addRoom(room);
        if (isSuccess) {
            // 添加成功，重定向到列表页
            resp.sendRedirect(req.getContextPath() + "/listRooms");
        } else {
            // 添加失败，转发回添加页并提示
            req.setAttribute("addMsg", "添加失败，请重新填写！");
            req.getRequestDispatcher("/add.jsp").forward(req, resp);
        }
    }
}